﻿/**********************************************
 * 功能名称：作业中心与目标中间体的路径设置
编写人：  杨莹
建立时间： 杨 2012年7月6日
说明：
根据操作员的责任中心范围，列表显示当前作业中心的中间体。
选择当前作业中心的目标中间体。
 *
 * 
 * 
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

namespace Hisun.View.CostCenterPath
{
    public partial class CostCenterToPIPPath : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                dropCostCenterSelect.Items.Clear();
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                HiddenField_CurrentCenterId.Value = "";

                MyEditState("Browse");

                Panel_PIPList.Visible = false;
           
            }
        }

        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => p.是否直接成本 == true && p.停用 == false && p.类型 == "A");


            foreach (var MylistItem in myCenterFilters)
            {

                if (MylistItem.作业中心名称.Contains(TextBox_FilterText.Text))
                {
                    ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                    dropCostCenterSelect.Items.Add(InsertItem);
                }
            }


        }

        //双击按钮的时候，填充当前的作业中心列表。
        protected void Button_fullCenterList_Click(object sender, EventArgs e)
        {
            var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
            dropCostCenterSelect.Items.Clear();
            ListItem InsertItem = new ListItem("请选择", "");
            dropCostCenterSelect.Items.Add(InsertItem);
            FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

            HiddenField_CurrentCenterId.Value = "";
        }


        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string CostCenterId = dropCostCenterSelect.SelectedValue;

            HiddenField_CurrentCenterId.Value = CostCenterId;

            BindMyData(CostCenterId);

            MyEditState("Browse");

            
        }

        private void MyEditState(string MyState)
        {
            switch (MyState)
	 
            {
                case "Add":
                    {
                        Panel_Select.Visible = true;
                        Panel_PIPList.Visible = false;

                        TextBox_FilterText.Enabled = false;
                        dropCostCenterSelect.Enabled = false;
                        Button_fullCenterList.Enabled = false;
                        Button_Query.Enabled = false;


                        break;
                    }
                case "Browse":
                    {
                        Panel_Select.Visible = false;
                        Panel_PIPList.Visible = true;
                        TextBox_CenterName.Text = "";
                        TextBox_ProductId.Text = "";
                        TextBox_FilterText.Enabled = true;
                        dropCostCenterSelect.Enabled = true;
                        Button_fullCenterList.Enabled = true;
                        Button_Query.Enabled = true;

                        break;
                    }
		         
           }
        
        
        
        }

        private void BindMyData(string CostCenterId)
        {

            int MyCenterId = Convert.ToInt32(CostCenterId);

            var MyPIPQuery = from a in db.TABC_JS_CostCenterToPIPPathFramework
                             join b in db.TABC_JS_ProductInProcess on a.中间体ID equals b.中间体ID 
                             join d in db.TABC_JS_Product on b.产品ID equals d.产品账户ID
                             join c in db.TABC_JS_CostCenter on a.作业中心ID equals c.作业中心ID
                             where a.作业中心ID == MyCenterId
                             orderby d.产品账户ID
                             select new
                             {
                                 ID = a.ID,
                                
                                作业中心ID = a.作业中心ID,
                                作业中心名称 = c.作业中心名称,
                                 中间体ID = a.中间体ID,
                                 中间体名称 = b.中间体名称,
                                 成本账户ID = d.产品账户ID,
                                 成本账户名称 = d.产品账户名称,
                                 系数 = a.系数,
                                 填充码 = a.填充码
                                 
                             };



          
                GridView_PIPList.DataSource = MyPIPQuery;
                GridView_PIPList.DataBind();


                if (MyPIPQuery.Count() == 0)
                {

                    alert = "<script>alert('当前查询结果为空');</script>";
                    HiddenField_FullDriverValue.Value = "0";
                    return;

                }

                switch (MyPIPQuery.First().填充码)
                {
                    case 1:
                        {
                            //填充当前目标成本账户的原材料投入数

                            Label_FullDriver.Text = "动因量自动填充方式:" + "原材料金额";
                      
                            HiddenField_FullDriverValue.Value = "1";
                            break;
                        }
                    case 2:
                        {
                            //填充当前目标中间体的人工投入数
                            Label_FullDriver.Text = "动因量自动填充方式:" + "人工费";
                            HiddenField_FullDriverValue.Value = "2";
                            break;
                        }
                    case 0:
                        {
                            Label_FullDriver.Text = "动因量自动填充方式:" + "无";
                            HiddenField_FullDriverValue.Value = "0";
                            break;
                        }



                }



        }


        protected void Button_Add_Click(object sender, EventArgs e)
        {
            MyEditState("Add");
            Panel_SelectGridList.Visible = false;
            DropDownList_DriverFullOption.SelectedValue = HiddenField_FullDriverValue.Value;

        }

        #region  选择中间体的代码。

        private void BindData_SelectPIP()
        {

            string ProductId = TextBox_ProductId.Text;
            string CenterName = TextBox_CenterName.Text;


            var MyPIPQuery_Select = from a in db.TABC_JS_ProductInProcess
                             join c in db.TABC_JS_CostCenter on a.作业中心ID equals c.作业中心ID
                             where (ProductId == ""? true:a.产品ID.StartsWith(ProductId))
                             && (CenterName == ""?true:c.作业中心名称.Contains(CenterName))
                             orderby a.产品ID
                             select new
                             {
                                 ID = a.ID,
                                 成本账户ID = a.产品ID,
                                 作业中心ID = a.作业中心ID,
                                 作业中心名称 = c.作业中心名称,
                                 中间体ID = a.中间体ID,
                                 中间体名称 = a.中间体名称
                             };



          
                GridView_SelectPIP.DataSource = MyPIPQuery_Select;
                GridView_SelectPIP.DataBind();



                if (MyPIPQuery_Select.Count() == 0)
                {
                    alert = "<script>alert('当前查询结果为空');</script>";
                    Panel_SelectGridList.Visible = false;

                }
                else
                {
                    Panel_SelectGridList.Visible = true;
                }


        }



        protected void Button_SelectQuery_Click(object sender, EventArgs e)
        {
            BindData_SelectPIP();
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);
            int MyFullDriverId = Convert.ToInt32(HiddenField_FullDriverValue.Value);
          
            try{
                for (int i = 0; i < GridView_SelectPIP.Rows.Count; i++)
                {
                    GridViewRow MyRow = GridView_SelectPIP.Rows[i];
                    bool Is_Selected = ((CheckBox)MyRow.Cells[5].FindControl("CheckBox_SelectPIP")).Checked;
                    if (Is_Selected)
                    {
                        int CenterId_Select = Convert.ToInt32(MyRow.Cells[1].Text);
                        string PIP_Select = MyRow.Cells[3].Text;

                        var IsExist = from a in db.TABC_JS_CostCenterToPIPPathFramework
                                      where a.作业中心ID == MyCenterId && a.目标中心ID == CenterId_Select
                                      && a.中间体ID == PIP_Select
                                      select a;
                        if (IsExist.Count() == 0)
                        {

                            TABC_JS_CostCenterToPIPPathFramework AddRow = new TABC_JS_CostCenterToPIPPathFramework();
                            AddRow.公司ID = "hisun";
                            AddRow.作业中心ID = MyCenterId;
                            AddRow.中间体ID = PIP_Select;
                            AddRow.账套ID = "001";
                            AddRow.目标中心ID = CenterId_Select;
                            AddRow.成本动因ID = 14;
                            AddRow.录入人 = Session[ServerVars.sysSessionUserName].ToString();
                            AddRow.系数 = 1;
                            AddRow.填充码 = MyFullDriverId;

                            db.TABC_JS_CostCenterToPIPPathFramework.InsertOnSubmit(AddRow);
                            db.SubmitChanges();
                        }


                    }

                   }


                
                          alert = "<script>alert('保存成功');</script>";
                          BindMyData(MyCenterId.ToString());
                          MyEditState("Browse");
                    
                }catch
                {
                                   alert = "<script>alert('保存失败');</script>";
                
                
                }

            
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            MyEditState("Browse");
        }

        #endregion

        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {
           int MyID = Convert.ToInt32(e.CommandArgument);

           var DelRow = from a in db.TABC_JS_CostCenterToPIPPathFramework
                        where a.ID == MyID
                        select a;
           if (DelRow.Count() == 1)
           {

               try {
                   db.TABC_JS_CostCenterToPIPPathFramework.DeleteOnSubmit(DelRow.Single());
                   db.SubmitChanges();

                    alert = "<script>alert('删除成功');</script>";


                         int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);
                          BindMyData(MyCenterId.ToString());
                          MyEditState("Browse");

               }
               catch {  alert = "<script>alert('删除失败');</script>";}
           
           
           }
        }

        protected void GridView_PIPList_OnRowCommand(object sender, GridViewEditEventArgs e)
        {
            GridView_PIPList.EditIndex = e.NewEditIndex;


            //此处需要触发gfidview的数据绑定。注意要考虑页码。部分数据绑定。
            int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);
            BindMyData(MyCenterId.ToString());

        }

        protected void GridView_PIPList_OnRowCancel(object sender, GridViewCancelEditEventArgs e)
        {
            GridView_PIPList.EditIndex = -1;

            //此处需要触发gfidview的数据绑定。注意要考虑页码。部分数据绑定。
            int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);
            BindMyData(MyCenterId.ToString());
        }
        protected void GridView_PIPList_OnRowUpdate(object sender, GridViewUpdateEventArgs e)
        {
            
            int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);
            GridViewRow gvr = GridView_PIPList.Rows[e.RowIndex];
            string PIPID = ((Label)gvr.FindControl("Label4")).Text;
            var query = (from a in db.TABC_JS_CostCenterToPIPPathFramework where a.作业中心ID == MyCenterId && a.中间体ID == PIPID select a).Single();
            query.系数 = Convert.ToDouble(((TextBox)gvr.FindControl("TextBox_Index")).Text);

            try
            {
                db.SubmitChanges();

                //将当前编缉行改成非编辑状态，重新绑定数据。考虑到页面，部分绑定数据。
                GridView_PIPList.EditIndex = -1;
                BindMyData(MyCenterId.ToString());
            }
            catch
            {

            }

        }


        //更新当前作业中心的填充码
        protected void Button1_Click(object sender, EventArgs e)
        {

            int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);
            int OldFullDriverId = Convert.ToInt32(HiddenField_FullDriverValue.Value);
            int NewFullDriverId = Convert.ToInt32(DropDownList_DriverFullOption.SelectedValue);

            if (NewFullDriverId != OldFullDriverId)
            {
                var AllRows = from a in db.TABC_JS_CostCenterToPIPPathFramework
                              where a.作业中心ID == MyCenterId
                              select a;
                if (AllRows.Count() > 0)
                {
                    foreach (var MyRow in AllRows)
                    {

                        MyRow.填充码 = NewFullDriverId;
                      

                    }
                    db.SubmitChanges();
                    BindMyData(MyCenterId.ToString());
                    MyEditState("Browse");
                }
            }
        }






 

    }
}
